home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / Javacup / UNUAM4CS.TAR / unlimited / UNUAM4CS / SliceVolumeFilter.java < prev    next >
Encoding:
Java Source  |  1996-05-21  |  6.0 KB  |  182 lines

  1. /*
  2.  * @(#)/SliceVolumeFilter.java    1.6 96/03/31 by Andrew Barclay abb@nuccard.eushc.org
  3.  *
  4.  * Copyright (c) 1995 Andrew B. Barclay All Rights Reserved.
  5.  *
  6.  * derived from :
  7.  * @(#)CropImageFilter.java    1.2 95/08/29 Jim Graham
  8.  *
  9.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  10.  *
  11.  * Permission to use, copy, modify, and distribute this software
  12.  * and its documentation for NON-COMMERCIAL purposes and without
  13.  * fee is hereby granted provided that this copyright notice
  14.  * appears in all copies. Please refer to the file "copyright.html"
  15.  * for further important copyright and licensing information.
  16.  *
  17.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  18.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  19.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  20.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  21.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  22.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  23.  */
  24.  
  25. import java.awt.image.ImageFilter ;
  26. import java.awt.image.ImageConsumer ;
  27. import java.awt.image.ColorModel ;
  28. import java.awt.Point ;
  29. import java.awt.Rectangle ;
  30. import java.util.Hashtable ;
  31.  
  32. /**
  33.  * An abstract ImageFilter class for sliceing volume images.
  34.  * This class extends the basic ImageFilter Class to extract a given
  35.  * slice of an existing Volume and provide a source for a
  36.  * new image containing just the extracted slice.  It is meant to
  37.  * be used in conjunction with a FilteredImageSource object to produce
  38.  * sliced versions of existing volumes.
  39.  *
  40.  * @see XSliceVolumeFilter
  41.  * @see YSliceVolumeFilter
  42.  * @see ZSliceVolumeFilter
  43.  * @see CropImageFilter
  44.  * @see FilteredImageSource
  45.  * @see ImageFilter
  46.  *
  47.  * @version    1.6 96/03/31
  48.  * @author     Andrew Barclay
  49.  */
  50.  
  51. abstract class SliceVolumeFilter extends ImageFilter {
  52.     final static int vdim = 3 ;
  53.     Point srcSlices[] ;
  54.     int voldims[] = new int[3] ;
  55.     // Not used yet...
  56.     double ul[] ;
  57.     double ll[] ;
  58.     double ur[] ;
  59.  
  60.     char type ;
  61.     double slice ;
  62.     int width ;
  63.     int height ;
  64.     boolean debug ;
  65.     
  66.     /**
  67.      * Construct a SliceVolumeFilter that extracts a slice of the source
  68.      * Volume specified by the ul, ll and ur parameters.
  69.      * @param srcSlices[] the array of x,y positions of the source image slices
  70.      * @param volWidth the width of the source volume
  71.      * @param volHeight the height of the source volume
  72.      * @param slice the slice number to be extracted
  73.      * @param width the width of the slice to be extracted
  74.      * @param height the height of the slice to be extracted
  75.      */
  76.     SliceVolumeFilter( Point srcSlices[], int volWidth, int volHeight,
  77.         double slice, int width, int height ) {
  78.     // info about the source volume (assuming it's arranged as zslices)
  79.     this.srcSlices = srcSlices ;
  80.     this.voldims[0] = volWidth ;
  81.     this.voldims[1] = volHeight ;
  82.     this.voldims[2] = srcSlices.length ;
  83.  
  84.     // info about position of slice in source volume.
  85.     this.slice = slice ;
  86.     this.width = width ;
  87.     this.height = height ;
  88.     dbg( "new SliceVolumeFilter: voldims="+voldims+", type="+type+", wh="+width+","+height ) ;
  89.     dbg( "srcSlices="+srcSlices ) ;
  90.     }
  91.  
  92.     /**
  93.      * Construct a SliceVolumeFilter that extracts a slice of the source
  94.      * Volume specified by the ul, ll and ur parameters.
  95.      * @param srcSlices[] the array of x,y positions of the source image slices
  96.      * @param volWidth the width of the source volume
  97.      * @param volHeight the height of the source volume
  98.      * @param ul the upper left coordinate of the slice to be extracted
  99.      * @param ll the lower left coordinate of the slice to be extracted
  100.      * @param ur the upper right coordinate of the slice to be extracted
  101.      * @param width the width of the slice to be extracted
  102.      * @param height the height of the slice to be extracted
  103.      */
  104.     SliceVolumeFilter( Point srcSlices[], int volWidth, int volHeight,
  105.         double ul[], double ll[], double ur[], int width, int height ) {
  106.     // info about the source volume (assuming it's arranged as zslices)
  107.     this.srcSlices = srcSlices ;
  108.     this.voldims[0] = volWidth ;
  109.     this.voldims[1] = volHeight ;
  110.     this.voldims[2] = srcSlices.length ;
  111.  
  112.     // info about position of slice in source volume.
  113.     this.ul = ul ;
  114.     this.ll = ll ;
  115.     this.ur = ur ;
  116.     this.width = width ;
  117.     this.height = height ;
  118.     dbg( "new SliceVolumeFilter: voldims="+voldims ) ;
  119.     }
  120.  
  121.     synchronized void dbg( String s ) {
  122.     if( debug ) {
  123.         System.out.println( s ) ;
  124.     }
  125.     }
  126.  
  127.     /**
  128.      * Pass the properties from the source object along after adding a
  129.      * property indicating the sliced plane.
  130.      */
  131.     public void setProperties(Hashtable props) {
  132.     props.put("slicevolume", this) ;
  133.     super.setProperties(props);
  134.     dbg( "SliceVolumeFilter.setProperties()" ) ;
  135.     }
  136.  
  137.     /**
  138.      * Override the source image's dimensions and pass the dimensions
  139.      * of the slice to the ImageConsumer.
  140.      * @see ImageConsumer
  141.      */
  142.     public void setDimensions(int w, int h) {
  143.     consumer.setDimensions(width, height) ;
  144.     dbg( "SliceVolumeFilter.setDimensions() to "+width+", "+height ) ;
  145.     }
  146.     
  147.     // Find the first slice that intersects the source region.
  148.     int firstZSlice( Rectangle sr ) {
  149.     int islice ;
  150.     Rectangle dr = new Rectangle( voldims[0], voldims[1] ) ;
  151.  
  152.     for( islice = 0 ; islice < voldims[2] ; islice++ ) {
  153.         // Does this slice intersect the source region?
  154.         dr.move( srcSlices[islice].x, srcSlices[islice].y ) ;
  155.         if( dr.intersects( sr ) ) {
  156.         break ;
  157.         }
  158.     }
  159.     return islice ;
  160.     }
  161.  
  162.     // Find the last slice that intersects the source region.
  163.     int lastZSlice( Rectangle sr ) {
  164.     int islice ;
  165.     Rectangle dr = new Rectangle( voldims[0], voldims[1] ) ;
  166.  
  167.     for( islice = voldims[2]-1 ; islice >= 0 ; islice-- ) {
  168.         // Does this slice intersect the source region?
  169.         dr.move( srcSlices[islice].x+1, srcSlices[islice].y+1 ) ;
  170.         if( dr.intersects( sr ) ) {
  171.         break ;
  172.         }
  173.     }
  174.  
  175.     return islice+1 ;
  176.     }
  177.  
  178.     // Really want setPixels to be abstract so that derived classes must
  179.     // implement, but can't because they are not abstract in the ImageFilter
  180.     // superclass...
  181. }
  182.